<template>
  <a-button
    type="default"
    @click="onClick"
    :disabled="disabled || loading"
    :loading="loading"
  >
    下载
  </a-button>
</template>
<script setup>
import { computed, ref } from 'vue'
import { message } from 'ant-design-vue'
import { downloadFileStructure } from '@/api/db.js'
import { downloadFormBlob } from '@/utils/util.js'
import { useDbType, useRowSelection } from './provide-context.js'
// import { set } from '@kitware/vtk.js/macros.js'

let rowSelection = useRowSelection()
let disabled = computed(() => {
  return rowSelection.selectedRowKeys.value.length === 0
})

let dbType = useDbType()
let loading = ref(false)
async function onClick() {
  loading.value = true
  try {
    let ids = rowSelection.selectedRowKeys.value.map(id => {
      return id
    })

    downloadFileStructure({ ids }, dbType)
      .then(res => downloadFormBlob(res, false))
      .catch(() => {
        message.error(`下载失败<${id}>`)
      })
    delete rowSelection.getCheckboxProps
  } catch (e) {
    console.log(e)
  }
  loading.value = false
}
</script>
<style lang="less" scoped></style>
